package lsl.leetCode.year2023.test0206;

import lsl.leetCode.TreeNode;

/**
 * 112.路径总和
 *
 * @author Zed
 * @description
 * @date 2023/2/6 20:05
 */
public class test112 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(5);
        TreeNode treeNod2 = new TreeNode(4);
        TreeNode treeNod3 = new TreeNode(8);
        TreeNode treeNod4 = new TreeNode(11);
        TreeNode treeNod5 = new TreeNode(13);
        TreeNode treeNod6 = new TreeNode(4);
        TreeNode treeNod7 = new TreeNode(7);
        TreeNode treeNod8 = new TreeNode(2);
        TreeNode treeNod9 = new TreeNode(4);
        TreeNode treeNod10 = new TreeNode(1);
        treeNode1.left = treeNod2;
        treeNode1.right = treeNod3;
        treeNod2.left = treeNod4;
        treeNod4.left = treeNod7;
        treeNod4.right = treeNod8;
        treeNod3.left = treeNod5;
        treeNod3.right = treeNod6;
        treeNod9.right = treeNod10;
        System.out.println(hasPathSum(treeNode1, 22));
    }

    public static boolean hasPathSum(TreeNode root, int targetSum) {
        if(root==null){
            return false;
        }
        if(root.left==null && root.right==null){
            return root.val==targetSum;
        }
        boolean leftFlag=hasPathSum(root.left,targetSum-root.val);
        boolean rightFlag= hasPathSum(root.right,targetSum-root.val);
        if(leftFlag || rightFlag){
            return true;
        }
        return false;
    }
}
